package com.czk.springcloud.service.impl;

import com.czk.springcloud.service.IMessageProvide;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 实现类
 * EnableBinding(Source.class) 定义消息的推送管道
 *
 * @Author:ChenZhangKun
 * @Date: 2020/12/22 22:07
 */
@EnableBinding(Source.class)
public class MessageProviderImpl implements IMessageProvide {
    /**
     * 定义消息发送管道
     */
    @Resource
    private MessageChannel output;

    @Override
    public String send() {
        String serial = UUID.randomUUID().toString().replaceAll("-", "");
        // 构建消息并发送出去
        boolean send = output.send(MessageBuilder.withPayload(serial).build());
        if (send) {
            return "发送成功";
        }
        return "发送失败";
    }
}
